package com.ysd.lis.service.bac.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.common.Result;
import com.ysd.lis.entity.SysUser;
import com.ysd.lis.entity.bac.BacResistantPhenotypeDict;
import com.ysd.lis.entity.bac.BacResistantTypeDict;
import com.ysd.lis.entity.sta.StaResistantPhenotype;
import com.ysd.lis.mapper.bac.BacResistantPhenotypeDictMapper;
import com.ysd.lis.request.bac.BacGermBaseDictRequest;
import com.ysd.lis.service.bac.BacResistantPhenotypeDictService;
import com.ysd.util.RedisUserManager;
import com.ysd.util.ToolsUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author LuoXS
 * @date 2024/3/27
 * version 1.0
 */
@Service
public class BacResistantPhenotypeDictServiceImpl extends ServiceImpl<BacResistantPhenotypeDictMapper, BacResistantPhenotypeDict> implements BacResistantPhenotypeDictService {

    @Autowired
    BacResistantPhenotypeDictMapper mapper;


    @Override
    public Result add(BacResistantPhenotypeDict bacSiteOfInfectionDict) {
        Result result = Result.succ(1, "添加成功！", null);
        try {
            SysUser sysUser = RedisUserManager.getUser();
            if (ToolsUtils.isNotEmpty(sysUser)) {
                bacSiteOfInfectionDict.setCreator(sysUser.getCreator());
                bacSiteOfInfectionDict.setOrgId(sysUser.getOrgId());
            }
            boolean save = this.save(bacSiteOfInfectionDict);
            if (!save) {
                result = Result.fail("添加失败！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = Result.fail("添加失败！");
        }
        return result;
    }

    @Override
    public Result updateDict(BacResistantPhenotypeDict bacSiteOfInfectionDict) {
        boolean b = this.updateById(bacSiteOfInfectionDict);
        if (b) {
            return Result.succ(1, "编辑成功！", null);
        } else {
            return Result.fail("编辑失败！");
        }
    }

    @Override
    public Result pageList(BacGermBaseDictRequest.BacResistantPhenotypeDictPageParam pageParam) {
        Result result;
        try {
            MPJLambdaWrapper<BacResistantPhenotypeDict> mpjLambdaWrapper = new MPJLambdaWrapper<>();
            Page<BacResistantPhenotypeDict> page = new Page<>();
            page.setCurrent(pageParam.getPageIndex());
            page.setSize(pageParam.getPageSize());
            mpjLambdaWrapper
                    .selectAll(BacResistantPhenotypeDict.class)
                    .selectAs(BacResistantTypeDict::getName, BacResistantPhenotypeDict::getResistantTypeNa)
                    .leftJoin(BacResistantTypeDict.class,p->p.eq(BacResistantTypeDict::getCode,BacResistantPhenotypeDict::getResistantTypeNo).eq(BacResistantTypeDict::getDelFlag,0));
            // 参数
            if (!StringUtils.isEmpty(pageParam.getSearchValue())) {
                mpjLambdaWrapper.and(wrapper ->
                        wrapper.like(BacResistantPhenotypeDict::getName, pageParam.getSearchValue())
                                .or().like(BacResistantPhenotypeDict::getStandardName, pageParam.getSearchValue()));

            }
            mpjLambdaWrapper.eq(BacResistantPhenotypeDict::getDelFlag, 0);
            mpjLambdaWrapper.orderByAsc(BacResistantPhenotypeDict::getDisplayOrder);
            Page<BacResistantPhenotypeDict> labInstrSetupPage = mapper.selectPage(page, mpjLambdaWrapper);
            List<BacResistantPhenotypeDict> records = labInstrSetupPage.getRecords();
            if(ToolsUtils.isNotEmpty(records)){
                List<BacResistantPhenotypeDict> collect1 = records.stream()
                        .sorted(Comparator.comparing(bacResult -> {
                                    if (com.baomidou.mybatisplus.core.toolkit.StringUtils.isBlank(bacResult.getDisplayOrder())) {
                                        return Integer.MAX_VALUE;
                                    }
                                    return Integer.parseInt(bacResult.getDisplayOrder());
                                }))
                        .collect(Collectors.toList());
                labInstrSetupPage.setRecords(collect1);
            }
            result = Result.succ(labInstrSetupPage);
        } catch (Exception e) {
            e.printStackTrace();
            result = Result.fail("查询失败！");
        }
        return result;
    }

    @Override
    public Result delete(BacGermBaseDictRequest.BacResistantPhenotypeDictDeleteParam deleteParam) {
        Result result;

        try {
            if (ToolsUtils.isEmpty(deleteParam) || ToolsUtils.isEmpty(deleteParam.getIds())) {
                return Result.fail("入参不可为空！");
            }
            String[] array = deleteParam.getIds().split(",");
            boolean b = this.removeByIds(Arrays.asList(array));
            if (b) {
                result = Result.succ(1, "删除成功", null);
            } else {
                result = Result.fail(400, "删除失败", "");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = Result.fail(400, "删除失败", "");
        }
        return result;
    }

    @Override
    public Result findDictList(BacResistantPhenotypeDict param) {
        Result result;
        try {
            MPJLambdaWrapper<BacResistantPhenotypeDict> wrapper = new MPJLambdaWrapper<>();
            wrapper
                    .selectAll(BacResistantPhenotypeDict.class)
                    .selectAs(BacResistantTypeDict::getName, BacResistantPhenotypeDict::getResistantTypeNa)
                    .leftJoin(BacResistantTypeDict.class,p->p.eq(BacResistantTypeDict::getCode,BacResistantPhenotypeDict::getResistantTypeNo).eq(BacResistantTypeDict::getDelFlag,0));
            wrapper.eq(BacResistantPhenotypeDict::getDelFlag, 0)
                    .isNotNull(BacResistantPhenotypeDict::getName)
                    .isNotNull(BacResistantPhenotypeDict::getCode);

            // 参数
            if (!StringUtils.isEmpty(param.getKeywords())) {
                wrapper.and(wrapper1 ->
                        wrapper1.like(BacResistantPhenotypeDict::getName, param.getKeywords())
                                .or().like(BacResistantPhenotypeDict::getStandardName, param.getKeywords()));

            }
            SysUser sysUser = RedisUserManager.getUser();
            if (ToolsUtils.isNotEmpty(sysUser)) {
                wrapper.eq(BacResistantPhenotypeDict::getOrgId, sysUser.getOrgId());
            }

            List<BacResistantPhenotypeDict> dicts = mapper.selectList(wrapper);
            List<BacResistantPhenotypeDict> collect1 = dicts.stream()
                    .sorted(Comparator.comparing(bacResult -> {
                        if (com.baomidou.mybatisplus.core.toolkit.StringUtils.isBlank(bacResult.getDisplayOrder())) {
                            return Integer.MAX_VALUE;
                        }
                        return Integer.parseInt(bacResult.getDisplayOrder());
                    }))
                    .collect(Collectors.toList());
            result = Result.succ(collect1);
        } catch (Exception e) {
            e.printStackTrace();
            result = Result.fail("查询失败！");
        }
        return result;
    }

    @Override
    public Result staDataResistantPhenotype(List<StaResistantPhenotype> staResistantPhenotypes) {
        for (StaResistantPhenotype staResistantType : staResistantPhenotypes) {
            BacResistantPhenotypeDict bacResistantTypeDict = new BacResistantPhenotypeDict();
            BeanUtils.copyProperties(staResistantType, bacResistantTypeDict, "id");
            bacResistantTypeDict.setCode(staResistantType.getResiTypeNo());
            bacResistantTypeDict.setName(staResistantType.getResiTypeNa());
            bacResistantTypeDict.setStandardName(staResistantType.getStandardNa());
            bacResistantTypeDict.setDisplayOrder(staResistantType.getSeq());
            this.saveOrUpdate(bacResistantTypeDict, new LambdaQueryWrapper<BacResistantPhenotypeDict>().eq(BacResistantPhenotypeDict::getCode, bacResistantTypeDict.getCode()).eq(BacResistantPhenotypeDict::getDelFlag, 0));
        }
        return Result.succ(1, "同步成功！", null);
    }
}
